﻿using System;
using System.Linq;
using Loki.Engine;
using Loki.UI.Wpf;
using Luna.Model;
using Luna.Model.Configuration;
using Luna.UI.Install;
using Luna.UI.Wpf.Install;

namespace Luna.UI.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class LunaApp : BootStrapper<LunaModel, MainViewModel>
    {
        private SplashScreenWindow1 _Splash = new SplashScreenWindow1();

        public LunaApp()
        {
            Initializing += new System.EventHandler(LunaApp_Initializing);
            Initialized += new System.EventHandler(LunaApp_Initialized);
        }

        void LunaApp_Initialized(object sender, System.EventArgs e)
        {
            _Splash.CloseSplashScreen();
        }

        void LunaApp_Initializing(object sender, System.EventArgs e)
        {
            _Splash.Show();
        }

        public override void PreStart()
        {
            OnInitialized(EventArgs.Empty);

            // first install
            if (ApplicationModel.Install.GetCurrentState() == InstallState.New)
            {
                ApplicationModel.Install.Install();
            }

            // create first user or logon
            if (ApplicationModel.Security.Users.Count() == 0)
            {
                FirstUserViewModel L_FirstUserVM = ContextHelper.GetViewModel<FirstUserViewModel>();
                ContextHelper.GetView<FirstUserView>(L_FirstUserVM).Show();
            }
            else
            {
                LogonViewModel L_LogonVM = ContextHelper.GetViewModel<LogonViewModel>();
                ContextHelper.GetView<LogonView>(L_LogonVM).Show();
            }
        }
    }
}
